package it.xiuxian.lib.base

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.trello.rxlifecycle4.components.support.RxFragment

import it.xiuxian.lib.R


@Suppress("UNCHECKED_CAST")
abstract class BaseFragment<P : IPresenter<*>?> : RxFragment(), IBaseViewLife {
    @JvmField
    protected var mPresenter: P? = null
    private var inflate: View? = null
    private var dataBinding:ViewDataBinding?=null
    private var isVisible: Boolean? = false //是否可见
    private var isViewCreate = false //是否初始化完成
    private var isFirst = true //是否第一次加载

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        if (inflate == null) {
            dataBinding = DataBindingUtil.inflate(inflater,layoutId, container,false)
            inflate = dataBinding!!.root
            mPresenter = presenter
            mPresenter!!.attachView(this, requireActivity())
        }
        if (inflate!!.parent != null) {
            (inflate!!.parent as ViewGroup).removeView(inflate)
        }
        isViewCreate = true
        if(userVisibleHint){
            dispatchUserVisibleHint(true)
        }
        return inflate
    }

    override fun onDestroy() {
        mPresenter!!.detachView()
        mPresenter!!.cancelRequest()
        super.onDestroy()

    }
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initData()
        initEvent()
    }
  /* private fun lazyLoad(){
       if (!isViewCreate || !isVisible!! || !isFirst) {
           return
       }
       loadData()
       isFirst = false
    }*/


    private fun dispatchUserVisibleHint(visibleState:Boolean){
        if(isVisible==visibleState){
            return
        }
        isVisible = visibleState
        if(visibleState){
            loadData()
        }else{
            stopData()
        }
    }

    protected open fun stopData() {

    }

    protected open fun <T : ViewDataBinding?> getViewDataBinding(): T {
        return dataBinding as T
    }
    protected abstract val layoutId: Int
    protected abstract val presenter: P
    protected open fun initView() {}
    protected open fun initData() {}
    protected open fun initEvent() {}
    override fun showLoading(){
        if (activity != null && activity is BaseActivity<*>) {
            (activity as BaseActivity<*>?)!!.showLoading()
        }
    }

    override fun hideLoading() {
        if (activity != null && activity is BaseActivity<*>) {
            (activity as BaseActivity<*>?)!!.hideLoading()
        }
    }

    /**
     * 设置标题
     *
     * @param title
     */
    fun setTitle(title: String?) {
        if (TextUtils.isEmpty(title)) {
            return
        }
        val tvTitle = inflate!!.findViewById<TextView>(R.id.tvTitle)
        if (tvTitle != null) {
            tvTitle.text = title
        }
    }

    override fun getText(textView: TextView?): String {
        return textView!!.text.toString()
    }

    override fun color(id: Int): Int {
        return ContextCompat.getColor(requireContext(), id)
    }

    override fun drawable(id: Int): Drawable {
        return ContextCompat.getDrawable(requireContext(), id)!!
    }

    override fun setText(textView: TextView?, text: String?) {

            textView!!.text = text

        if (textView is EditText) {
            textView.setSelection(textView.text.length)
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        if(isViewCreate){
            if(!isVisible!!&&isVisibleToUser){
                dispatchUserVisibleHint(true)
            }else if(isVisible!!&&!isVisibleToUser){
                dispatchUserVisibleHint(false)
            }
        }


    }

    protected open fun loadData(){}

    override fun onResume() {
        super.onResume()
        if(!isVisible!!&&userVisibleHint){
            dispatchUserVisibleHint(true)
        }
    }
    override fun onPause(){
        super.onPause()
        if(isVisible!!&&!userVisibleHint){
            dispatchUserVisibleHint(false)
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
    }
}